/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.grupo.controler;

import com.grupo.model.VO.Prova;
import com.grupo.model.DAO.ProvaDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * @author Romulo
 */
@WebServlet(name = "Controlador", urlPatterns = {"/Controlador"})
public class Controlador extends HttpServlet {

    static String execute(HttpServletRequest request) {
	throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String nome = request.getParameter("nome");
      
        String telefone = request.getParameter("telefone");
        String op = request.getParameter("op");
        if(op.equals("atualizar")){
               //out.print("olá mundo!");
               out.print(nome);
               out.print(telefone);
               Integer id = new Integer(request.getParameter("id"));
               out.println("codigo:"+id);
                    try {
                        new ProvaDAO().update(new Prova(id,nome,telefone));
                          request.setAttribute("mensagem","update executado");
                                  request.getRequestDispatcher("index.jsp").forward(request, response);    
                        /*try {
                                /* Cliente c = new Cliente();
                                 Integer id = new Integer(request.getParameter("id"));
                                 c.setId(id);
                                 c.setNome(nome);
                                 c.setTelefone(telefone);
                                 new ClienteDAO().update(c);
                                 request.setAttribute("mensagem","update executado");
                                  request.getRequestDispatcher("index.jsp").forward(request, response);
                             } catch (Exception ex) {
                                 Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
                             }*/
                    } catch (Exception ex) {
                        Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
                    }
           
        }else if(op.equals("cad")){
                             
                  
              
                            out.println(nome);
                        out.println(telefone);
                 
                    
                    try {
                        
                       Prova c = new Prova();
                       c.setNome(nome);
                       c.setTelefone(telefone);
                       out.print(c.getNome());
                       out.print(c.getTelefone());
                       new ProvaDAO().cadastrar(c);
                       request.setAttribute("mensagem","cadastro executado");
                       request.getRequestDispatcher("index.jsp").forward(request, response);
                       
                    } catch (Exception ex) {
                        Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    
                   
                 
               
            
        }else if(op.equals("excluir")){
            int id=Integer.parseInt(request.getParameter("id"));
                    try {
                        new ProvaDAO().delete(new Prova(id));
                         request.setAttribute("mensagem","delete executado");
                         request.getRequestDispatcher("index.jsp").forward(request, response);
                    } catch (Exception ex) {
                        Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
                    }
            
            
        }
    
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

            processRequest(request, response);
   
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      
            processRequest(request, response);
    
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
